package com.fz.controller;

import com.fz.entity.Student;
import com.fz.mapper.Studentmapper;
import com.fz.mybatis.MyHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by webrx on 2017-09-06.
 */
@Controller
@RequestMapping("student")
public class Students {
    //显示数据
    @RequestMapping("/shows")
    public String queryall(Model model){
        //调用工具类连接数据库
        MyHelper mh=new MyHelper();
        SqlSession ss=mh.getSession();
        //调用studentmapper接口
        Studentmapper st=ss.getMapper(Studentmapper.class);
        model.addAttribute("student",st.queryall());
        return "show";
    }

    //显示数据分页
       @RequestMapping("/pages")
       public String showw(HttpServletRequest req, Model model){
        //调用工具类连接数据库
        MyHelper mh=new MyHelper();
        SqlSession ss=mh.getSession();
        //调用studentmapper接口
        Studentmapper st=ss.getMapper(Studentmapper.class);
        String s=req.getParameter("p");
        System.out.println(s);
        //当前页
        int count=s==null?1:Integer.parseInt(s);
        //每页显示的个数
        int size=3;
        //总条数
        int getcount=st.getcount();
        //总页数
        int pagecount=getcount%size==0? getcount/size:getcount/size+1;
        if(count<1) count=1;
        if(count>pagecount) count =pagecount;
        int start=count*size-size;
        model.addAttribute("student",st.querypage(start,size));
        model.addAttribute("count",count);
        model.addAttribute("pagecount",pagecount);
        return "page";
    }

    //删除数据
    @RequestMapping("/delt/{id}/{p}")
    public String del(@PathVariable("id") int id,@PathVariable("p") int p){
        //调用工具类连接数据库
        MyHelper mh=new MyHelper();
        SqlSession ss=mh.getSession();
        //调用studentmapper接口
        Studentmapper st=ss.getMapper(Studentmapper.class);
        st.deleteById(id);
        ss.commit();
        return "redirect:/student/pages?p="+p;
       }

    //修改数据之前返回的页面
    @RequestMapping("/upshows/{id}/{p}")
    public String upshowss(@PathVariable("id") int id,Model model,@PathVariable("p") int p){
        //调用工具类连接数据库
        MyHelper mh=new MyHelper();
        SqlSession ss=mh.getSession();
        //调用studentmapper接口
        Studentmapper st=ss.getMapper(Studentmapper.class);
        model.addAttribute("students",st.upquery(id));
        model.addAttribute("p",p);
        return "oo";
    }

    //修改数据返回后的页面
    @RequestMapping(value = "/updates",method = RequestMethod.POST)
    public String upo(Student student, int p){
        //调用工具类连接数据库
        MyHelper mh=new MyHelper();
        SqlSession ss=mh.getSession();
        //调用studentmapper接口
        Studentmapper st=ss.getMapper(Studentmapper.class);
        st.update(student);
        ss.commit();
        return "redirect:/student/pages?p="+p;
    }

    //点击添加数据返回的视图
    @RequestMapping("/ins/{p}")
    public String inte(@PathVariable("p") int p,Model model){
        model.addAttribute("p",p);
        return "insert";
    }

    //添加数据完之后返回的页面
    @RequestMapping("/inserts/{p}")
    public String inn(Student student,@PathVariable("p") int p){
        //调用工具类连接数据库
        MyHelper mh=new MyHelper();
        SqlSession ss=mh.getSession();
        //调用studentmapper接口
        Studentmapper st=ss.getMapper(Studentmapper.class);
        //判断添加数据满之后 分页去下一页
        int a = st.getcount();
        int aa = a%3;
        if(aa==0){
            p++;
        }
        st.insert(student);
        ss.commit();
        return "redirect:/student/pages?p="+p;
    }
}
